package org.example.group.service.Impl;

import org.example.cassell_client.service.UserServiceClient;
import org.example.group.entity.Group;
import org.example.group.entity.GroupUser;
import org.example.group.repository.GroupRepository;
import org.example.group.service.GroupService;
import org.example.message.entity.Message;
import org.example.message.repository.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@EnableFeignClients(basePackages = "org.example.cassell_client.service")
public class GroupServiceImpl implements GroupService {
    private final GroupRepository groupRepository;
    private final UserServiceClient userServiceClient;
    private final MessageRepository messageRepository;

    public GroupServiceImpl(GroupRepository groupRepository, UserServiceClient userServiceClient, MessageRepository messageRepository) {
        this.groupRepository = groupRepository;
        this.userServiceClient = userServiceClient;
        this.messageRepository = messageRepository;
    }

    @Override
    public List<Group> getgrouplist(String UserEmail){
        List<Group> list= groupRepository.getgrouplist(UserEmail);
        return list;
    }
    @Override
    public List<Message> messagelist(){
        List<Message> list= messageRepository.messagelist();
        return list;
    }

    @Override
   public void addGroupMessage(Message message){
        messageRepository.addMessage(message);
    }

    @Override
    public int addGroup(String groupName, String groupBullder, String groupPicture) {
        Group group = new Group();
        group.setGroupName(groupName);
        group.setGroupBuilder(groupBullder);
        group.setGroupPicture(groupPicture);
        groupRepository.addGroup(group); // 使用useGeneratedKeys获取ID
        System.out.println(userServiceClient.getUserPicture(groupBullder));
        // 2. 加入群组
        return groupRepository.joinGroup(group.getGroupId(), groupBullder,userServiceClient.getUserPicture(groupBullder));
    }

    @Override
    public int joinGroup(Integer groupId, String userEmail) {
        System.out.println(userServiceClient.getUserPicture(userEmail));
        return groupRepository.joinGroup(groupId,userEmail,userServiceClient.getUserPicture(userEmail));
    }

    @Override
    public Group selectGroup(String groupId) {
        return groupRepository.selectGroup(groupId);
    }

    @Override
    public int outGroup(Integer groupId, String userEmail) {
        return groupRepository.outGroup(groupId,userEmail);
    }

    @Override
    public List<GroupUser> getUserByGroup(String groupId) {
        return groupRepository.getUserByGroup( groupId);
    }

}
